﻿(function () {
    'use strict';
    var serviceId = 'httpRequestSvc';

    angular.module("common").factory(serviceId, ['$http', '$q', httpRequestSvc]);

    function httpRequestSvc($http, $q) {

        var promiseObj = function (url, method, data, accessToken) {
            var deferred = $q.defer();

            $http({
                method: method,
                url: url,
                data: data,
                headers: { "Authorization": "Bearer " + accessToken, },
                contentType: "application/json",
            }).success(function (reponse) {
                deferred.resolve(reponse);
            }).error(function (err) {
                deferred.reject(err);
            });

            return deferred.promise;
        };

        return {
            getJson: function (url, accessToken) {
                return promiseObj(url, "GET", accessToken);
            },
            postJson: function (url, data, accessToken) {
                return promiseObj(url, "POST", data, accessToken);
            },
            putJson: function (url, data, accessToken) {
                return promiseObj(url, "PUT", data, accessToken);
            }
        };

    }
})();
